package cn.iocoder.yudao.module.project.controller.admin.deptcompletedlog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.project.controller.admin.deptcompletedlog.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.deptcompletedlog.DeptCompletedLogDO;
import cn.iocoder.yudao.module.project.service.deptcompletedlog.DeptCompletedLogService;

@Tag(name = "管理后台 - 课室完成记录")
@RestController
@RequestMapping("/project/dept-completed-log")
@Validated
public class DeptCompletedLogController {

    @Resource
    private DeptCompletedLogService deptCompletedLogService;

    @PostMapping("/create")
    @Operation(summary = "创建课室完成记录")
    @PreAuthorize("@ss.hasPermission('project:dept-completed-log:create')")
    public CommonResult<Integer> createDeptCompletedLog(@Valid @RequestBody DeptCompletedLogSaveReqVO createReqVO) {
        return success(deptCompletedLogService.createDeptCompletedLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课室完成记录")
    @PreAuthorize("@ss.hasPermission('project:dept-completed-log:update')")
    public CommonResult<Boolean> updateDeptCompletedLog(@Valid @RequestBody DeptCompletedLogSaveReqVO updateReqVO) {
        deptCompletedLogService.updateDeptCompletedLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课室完成记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('project:dept-completed-log:delete')")
    public CommonResult<Boolean> deleteDeptCompletedLog(@RequestParam("id") Integer id) {
        deptCompletedLogService.deleteDeptCompletedLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课室完成记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('project:dept-completed-log:query')")
    public CommonResult<DeptCompletedLogRespVO> getDeptCompletedLog(@RequestParam("id") Integer id) {
        DeptCompletedLogDO deptCompletedLog = deptCompletedLogService.getDeptCompletedLog(id);
        return success(BeanUtils.toBean(deptCompletedLog, DeptCompletedLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课室完成记录分页")
    @PreAuthorize("@ss.hasPermission('project:dept-completed-log:query')")
    public CommonResult<PageResult<DeptCompletedLogRespVO>> getDeptCompletedLogPage(@Valid DeptCompletedLogPageReqVO pageReqVO) {
        PageResult<DeptCompletedLogDO> pageResult = deptCompletedLogService.getDeptCompletedLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeptCompletedLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课室完成记录 Excel")
    @PreAuthorize("@ss.hasPermission('project:dept-completed-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeptCompletedLogExcel(@Valid DeptCompletedLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeptCompletedLogDO> list = deptCompletedLogService.getDeptCompletedLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课室完成记录.xls", "数据", DeptCompletedLogRespVO.class,
                        BeanUtils.toBean(list, DeptCompletedLogRespVO.class));
    }

}